import axios from "axios";
import { computed, ref, watch } from "vue";
import { useUserStore } from "@/stores/userStore";
import { ElMessage } from "element-plus";
import type { Form } from "@/interface/formData";
import type { Announcement } from "@/interface/announcementData";


export default function () {
    const { useridentityfer } = useUserStore();
    const works = ref<Form[]>([]); //请求后数据集
    const announcements = ref<Announcement[]>([]);

    //存储订单中每个状态的type数量
    const workProcessConunt = ref<Record<string, Record<string, number>>>({});
    const workStatusCount = ref<Record<string, Record<string, number>>>({});
    const source = ref<
        { product: string; 零工: number; 短期兼职: number; 长期兼职: number }[]
    >([]); // 统计数据

    //订单进程 1-待接取 2-已截取 3-经行中 4-已完成  5-已结算
    const processCount = computed(() => {
        return works.value?.reduce((acc, curr) => {
            const process = curr.process;
            acc[process] = (acc[process] || 0) + 1; // 统计计数
            return acc;
        }, {} as Record<string, number>); // 类型声明为字符串键-数字值的对象
    });

    // 审核状态 1-待审核  2-审核通过 3-审核失败
    const statusCount = computed(() => {
        return works.value?.reduce((acc, curr) => {
            const status = curr.status;
            acc[status] = (acc[status] || 0) + 1; // 统计计数
            return acc;
        }, {} as Record<string, number>); // 类型声明为字符串键-数字值的对象
    });




    function countWorks(data: Form[]) {
        // debugger;
        // 初始化统计对象
        const processMap: Record<string, Record<string, number>> = {};
        const statusMap: Record<string, Record<string, number>> = {};

        data.forEach((work) => {
            const { process, status, jobType } = work;

            // 统计 process
            if (!processMap[process]) {
                processMap[process] = { "1": 0, "2": 0, "3": 0 };
            }
            processMap[process][jobType] =
                (processMap[process][jobType] || 0) + 1;

            // 统计 status
            if (!statusMap[status]) {
                statusMap[status] = { "1": 0, "2": 0, "3": 0 };
            }
            statusMap[status][jobType] = (statusMap[status][jobType] || 0) + 1;
        });

        workProcessConunt.value = processMap;
        workStatusCount.value = statusMap;
        updateSource();
    }

    // 更新 source 数据，确保所有数据已准备好后再赋值
    function updateSource() {
        source.value = [
            {
                product: "已发布职位",
                零工:
                    (workStatusCount.value["1"]?.["1"] || 0) +
                    (workStatusCount.value["2"]?.["1"] || 0) +
                    (workStatusCount.value["3"]?.["1"] || 0),
                短期兼职:
                    (workStatusCount.value["1"]?.["2"] || 0) +
                    (workStatusCount.value["2"]?.["2"] || 0) +
                    (workStatusCount.value["3"]?.["2"] || 0),
                长期兼职:
                    (workStatusCount.value["1"]?.["3"] || 0) +
                    (workStatusCount.value["2"]?.["3"] || 0) +
                    (workStatusCount.value["3"]?.["3"] || 0),
            },
            {
                product: "待审核职位",
                零工: workStatusCount.value["1"]?.["1"] || 0,
                短期兼职: workStatusCount.value["1"]?.["2"] || 0,
                长期兼职: workStatusCount.value["1"]?.["3"] || 0,
            },
            {
                product: "已经申请",
                零工: workProcessConunt.value["2"]?.["1"] || 0,
                短期兼职: workProcessConunt.value["2"]?.["2"] || 0,
                长期兼职: workProcessConunt.value["2"]?.["3"] || 0,
            },
            {
                product: "进行中",
                零工: workProcessConunt.value["3"]?.["1"] || 0,
                短期兼职: workProcessConunt.value["3"]?.["2"] || 0,
                长期兼职: workProcessConunt.value["3"]?.["3"] || 0,
            },
        ];

    }


    const ChartData = ref({
        dimensions: ["product", "零工", "短期兼职", "长期兼职"],
        source: source.value,
    });

    watch(source, () => {
        ChartData.value.source = [...source.value]
    }, { deep: true })


    //请求后端——获取工作信息
    function getWorks() {
        axios
            .get(`/api/labor/works/${useridentityfer}`)
            .then((response) => {
                if (response.data.message != "数据为空") {
                    works.value = response.data.data;
                    countWorks(works.value);
                }
            })
            .catch((error) => {
                ElMessage.error(
                    "职位数据请求失败：" +
                    (error.response?.data?.message || "服务器错误")
                );
            });
    }


    //请求后端——获取公告信息
    function getannouncements() {
        const date = new Date();
        const month = date.getMonth() + 1;
        axios
            .get(`/api/labor/announcements/${month}`)
            .then((response) => {
                announcements.value = response.data.data;
            })
            .catch((error) => {
                ElMessage.error(
                    "公告请求失败: " +
                    (error.response?.data?.message || "服务器错误")
                );
            });
    }

    const statusCard = computed(() => {
        return {
            successReviewNumber: statusCount.value["2"], //审核通过
            failReviewNumber: statusCount.value["3"], //审核失败
            receivedNumbe: processCount.value["2"], //已接取
        };
    });





    return {
        ChartData,
        announcements,
        statusCard,
        getWorks,
        getannouncements,
    };
}
